home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / interapplication comm / moreappleevents / objecthelpers.c < prev    next >
Encoding:
Text File  |  2000-06-23  |  10.2 KB  |  394 lines

  1. /*
  2.     File:        ObjectHelpers.c
  3.  
  4.     Contains:    Functions to help you when you are working with Apple event objects.
  5.  
  6.     Written by: Andy Bachorski    
  7.  
  8.     Copyright:    Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/21/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23. //    Conditionals to setup the build environment the way we like it.
  24. #include "PrivateConditionals.h"
  25.  
  26.  
  27. //**********    Universal Headers        ****************************************
  28.  
  29. #include <AERegistry.h>
  30. #include <AEObjects.h>
  31. #include <AEPackObject.h>
  32. #include <Aliases.h>
  33. #include <Icons.h>
  34. #include <Processes.h>
  35.  
  36. #if UNIVERSAL_INTERFACES_VERSION >= 0x0300
  37.     #include <FinderRegistry.h>
  38. #else
  39.     #include "FinderRegistry.h"
  40. #endif
  41.  
  42. //**********    Project Headers            ****************************************
  43.  
  44. #include "ObjectHelpers.h"
  45. #include "AEHelpers.h"
  46.  
  47.  
  48. //**********    Private Prototypes        ****************************************
  49.  
  50. static    pascal    OSErr    MyIconAction( ResType theIconType,
  51.                               const Handle *theIcon,
  52.                               void *myDataPtr);
  53. /*
  54.     Used by OHMakeIconFamilyRecord, passed to ForEachIconDo as the IconAction
  55.     function. Puts each icon in an icon suite into the descriptor record passed
  56.     in the myDataPtr parameter.
  57. */
  58.  
  59. //******************************************************************************
  60.  
  61. pascal    OSErr    OHAddAliasParameterFromFSS( const FSSpecPtr fssPtr,
  62.                                                 DescType keyword,
  63.                                                 AERecord *theRecordPtr )
  64. {
  65.     OSErr            anErr = noErr;
  66.     AliasHandle        aliasHandle;
  67.     
  68.     anErr = NewAlias( nil, fssPtr, &aliasHandle);
  69.     if ( anErr == noErr  &&  aliasHandle == nil )
  70.     {
  71.         anErr = paramErr;
  72.     }
  73.     
  74.     if ( anErr == noErr )
  75.     {
  76.         char    handleState;
  77.         
  78.         handleState = HGetState( (Handle)aliasHandle );
  79.         HLock( (Handle)aliasHandle );
  80.         
  81.         anErr = AEPutParamPtr( theRecordPtr, keyword, typeAlias,
  82.                                *aliasHandle, (*aliasHandle)->aliasSize);
  83.         
  84.         HSetState( (Handle)aliasHandle, handleState );
  85.         DisposeHandle( (Handle)aliasHandle );
  86.     }
  87.         
  88.     return anErr;
  89. }//end AddAliasParameterFromFSS
  90.  
  91. //******************************************************************************
  92.  
  93. pascal    OSErr    OHMakeAliasDescFromFSSpec( const FSSpecPtr fssPtr,
  94.                                                   AEDesc *aliasDescPtr )
  95. {
  96.     OSErr            anErr = noErr;
  97.     AliasHandle        aliasHandle;
  98.     
  99.     anErr = NewAlias( nil, fssPtr, &aliasHandle);
  100.     if ( anErr == noErr  &&  aliasHandle == nil )
  101.     {
  102.         anErr = paramErr;
  103.     }
  104.  
  105.     if ( anErr == noErr )
  106.     {
  107.         anErr = OHMakeAliasDesc( aliasHandle, aliasDescPtr );
  108.         DisposeHandle( (Handle)aliasHandle );
  109.     }
  110.         
  111.     return anErr;
  112. }//end MakeAliasObject
  113.  
  114. //******************************************************************************
  115.  
  116. pascal    OSErr    OHMakeAliasDesc( const AliasHandle aliasHandle,
  117.                                         AEDesc *aliasDescPtr )
  118. {
  119.     OSErr    anErr = noErr;
  120.     
  121.     char    handleState = HGetState( (Handle)aliasHandle );
  122.     HLock( (Handle)aliasHandle );
  123.     
  124.     anErr = AECreateDesc( typeAlias, *aliasHandle, GetHandleSize( (Handle)aliasHandle ), aliasDescPtr );
  125.     
  126.     HSetState( (Handle)aliasHandle, handleState );
  127.     
  128.     return anErr;
  129. }//end MakeAliasObject
  130.  
  131. //******************************************************************************
  132.  
  133. pascal    OSErr    OHMakeAliasObjectFromFSSpec( const FSSpecPtr fssPtr,
  134.                                                     AEDesc *containerObjPtr,
  135.                                                     AEDesc *aliasObjectPtr )
  136. {
  137.     OSErr        anErr = noErr;
  138.     AEDesc        aliasDesc = { typeNull, nil };
  139.  
  140.     anErr = OHMakeAliasDescFromFSSpec( fssPtr, &aliasDesc );
  141.     if ( anErr == noErr )
  142.     {
  143.         anErr = CreateObjSpecifier( typeAlias, containerObjPtr, formAbsolutePosition,
  144.                                     &aliasDesc, false, aliasObjectPtr );
  145.         AEDisposeDesc( &aliasDesc );
  146.     }
  147.     
  148.     return anErr;
  149. }//end MakeAliasObject
  150.  
  151. //******************************************************************************
  152.  
  153. pascal    OSErr    OHMakeAliasObject( const AliasHandle aliasHandle,
  154.                                           AEDesc *containerObjPtr,
  155.                                           AEDesc *aliasObjectPtr )
  156. {
  157.     OSErr    anErr = noErr;
  158.     AEDesc    aliasDesc;
  159.  
  160.     anErr = OHMakeAliasDesc( aliasHandle, &aliasDesc );
  161.     if ( anErr == noErr )
  162.     {
  163.         anErr = CreateObjSpecifier( typeAlias, containerObjPtr, formAbsolutePosition,
  164.                                     &aliasDesc, false, aliasObjectPtr );
  165.         AEDisposeDesc( &aliasDesc );
  166.     }
  167.     
  168.     return anErr;
  169. }//end MakeAliasObject
  170.  
  171. //******************************************************************************
  172.  
  173. pascal    OSErr    OHMakePropertyObject( const DescType propType,
  174.                                              AEDesc *containerObjPtr,
  175.                                              AEDesc *propertyObjPtr )
  176. {
  177.     OSErr    anErr = noErr;
  178.     AEDesc    propDesc;
  179.     
  180.     anErr = AECreateDesc( typeType, &propType, sizeof( propType ), &propDesc );
  181.     if ( anErr == noErr )
  182.     {
  183.         anErr = CreateObjSpecifier( cProperty, containerObjPtr, formPropertyID,
  184.                                     &propDesc, false, propertyObjPtr );
  185.         AEDisposeDesc( &propDesc );
  186.     }
  187.     
  188.     return anErr;
  189. }//end MakePropertyObject
  190.  
  191. //******************************************************************************
  192.  
  193. pascal    OSErr    OHMakeProcessObject( const ProcessSerialNumber *psnPtr,
  194.                                             AEDesc *containerObjPtr,
  195.                                             AEDesc *psnObjPtr )
  196. {
  197.     OSErr    anErr = noErr;
  198.     AEDesc    psnDesc;
  199.     
  200.     anErr = AECreateDesc( typeProcessSerialNumber, psnPtr, sizeof( ProcessSerialNumber ), &psnDesc );
  201.     if ( anErr == noErr )
  202.     {
  203.         anErr = CreateObjSpecifier( cProperty, containerObjPtr, formPropertyID,
  204.                                     &psnDesc, false, psnObjPtr );
  205.         AEDisposeDesc( &psnDesc );
  206.     }
  207.     
  208.     return anErr;
  209. }//end MakePropertyObject
  210.  
  211. //******************************************************************************
  212.  
  213. pascal    OSErr    OHMakeSelectionObject( const DescType selection,
  214.                                               AEDesc *containerObjPtr,
  215.                                               AEDesc *selectionObject )
  216. {
  217.     OSErr    anErr = noErr;
  218.     
  219.     AEDesc    selectionDesc = { typeNull, nil };
  220.     
  221.     anErr = AECreateDesc( typeAbsoluteOrdinal, &selection, sizeof( selection ), &selectionDesc );
  222.     if ( anErr == noErr )
  223.     {
  224.         anErr = CreateObjSpecifier( cObject, containerObjPtr, formAbsolutePosition,
  225.                                     &selectionDesc, false, selectionObject );
  226.         AEDisposeDesc( &selectionDesc );
  227.     }            
  228.     return anErr;
  229. }
  230.  
  231. //******************************************************************************
  232.  
  233. pascal    OSErr    OHMakeIconSuite( const AEDescList *iconFamilyRecPtr,
  234.                                         Handle *iconSuitePtr )
  235. {
  236.     OSErr    anErr = noErr;
  237.  
  238.     const    long        iconTypesCnt = 6;
  239.     static    DescType    iconTypes[] = {    typeIconAndMask,
  240.                                         type8BitIcon,
  241.                                         type4BitIcon,
  242.                                         typeSmallIconAndMask,
  243.                                         typeSmall8BitIcon,
  244.                                         typeSmall4BitIcon };
  245.     
  246.     AEDescList    iconList = { typeNull, nil };
  247.     
  248. //    if ( FinderUsesIconFamily() )
  249.     if ( true )
  250.     {
  251.         anErr = AECoerceDesc( iconFamilyRecPtr, typeAERecord, &iconList );
  252.     }
  253.     else
  254.     {
  255.         iconList = *iconFamilyRecPtr;
  256.     }
  257.     
  258.     if ( anErr == noErr )
  259.     {
  260.         anErr = NewIconSuite( iconSuitePtr );
  261.         if ( anErr == noErr )
  262.         {
  263.             long    index;
  264.             AEDesc    iconDataDesc = { typeNull, nil };
  265.             
  266.             for ( index = 0; index < iconTypesCnt; index++ )
  267.             {
  268.                 anErr = AEGetKeyDesc( &iconList, iconTypes[ index ],
  269.                                       typeWildCard, &iconDataDesc );
  270.                 if ( anErr == noErr )
  271.                 {
  272.                     anErr = AddIconToSuite( iconDataDesc.dataHandle,
  273.                                             *iconSuitePtr, iconTypes[ index ] );
  274.                 }
  275.             }
  276.         }
  277.     }
  278.     AEDisposeDesc( &iconList );
  279.  
  280.     return anErr;
  281. }//end OHMakeIconSuite
  282.  
  283. //******************************************************************************
  284.  
  285. pascal    OSErr    MyIconAction( ResType theIconType,
  286.                               const Handle *theIcon,
  287.                               void *myDataPtr)
  288. {
  289.     OSErr    anErr = noErr;
  290.     
  291.     if ( *theIcon != nil )    // only add the icon if it's really there
  292.     {
  293.         AEDescList *iconFamilyRecPtr = (AEDescList*)myDataPtr;
  294.         
  295.         anErr = AEPutKeyPtr( iconFamilyRecPtr, theIconType, theIconType,
  296.                              **theIcon, GetHandleSize( *theIcon ) );
  297.     }
  298.         
  299.     return anErr;
  300. }//end MyIconAction
  301.  
  302. //******************************************************************************
  303.  
  304. pascal    OSErr    OHMakeIconFamilyRecord( const Handle iconSuite,
  305.                                          const IconSelectorValue iconSelector,
  306.                                                AEDescList *iconFamilyRecPtr )
  307. {
  308.     OSErr    anErr = noErr;
  309.     AEDescList    iconList = { typeNull, nil };
  310.     
  311.     static    IconActionUPP iconActionUPP;
  312.     
  313.     if ( iconActionUPP == nil )
  314.     {
  315.         iconActionUPP = NewIconActionProc( &MyIconAction );
  316.     }
  317.     
  318.     // create a record for the icon family
  319.     anErr = AECreateList( nil, 0, true, &iconList );
  320.     
  321.     if ( anErr == noErr )
  322.     {
  323.         ForEachIconDo( iconSuite, iconSelector,
  324.                        iconActionUPP, &iconList );
  325.         
  326. //        if ( FinderUsesIconFamily() )
  327.         if ( true )
  328.         {
  329.             anErr = AECoerceDesc( &iconList, typeIconFamily, iconFamilyRecPtr );
  330.         }
  331.         else
  332.         {
  333.             *iconFamilyRecPtr = iconList;
  334.         }
  335.         
  336.         AEDisposeDesc( &iconList );
  337.     }
  338.  
  339.     return anErr;
  340. }//end OHMakeIconFamilyRecord
  341.  
  342. //******************************************************************************
  343.  
  344. pascal    OSErr    OHMakePositionList( const Point position,
  345.                                      AEDescList * posListPtr )
  346. {
  347.     OSErr    anErr = noErr;
  348.     
  349.     anErr = AECreateList( nil, 0, false, posListPtr );
  350.     if ( anErr == noErr )
  351.     {
  352.         anErr = AEPutPtr( posListPtr, 0, typeInteger, &(position.h), sizeof( short ) );
  353.         if ( anErr == noErr )
  354.         {
  355.             anErr = AEPutPtr( posListPtr, 0, typeInteger, &(position.v), sizeof( short ) );
  356.         }
  357.     }
  358.     
  359.     return anErr;
  360. }//end OHMakePositionList
  361.  
  362. //******************************************************************************
  363.  
  364. // *******************************************
  365. // *****       AddTwoIntListToEvent      *****
  366. // *******************************************
  367. //    Creates an AEList containing 2 integers, and then adds the list to an Apple event.
  368.  
  369. /*
  370. OSErr AddTwoIntListToEvent( AppleEvent *theEvent, long param1, long param2 )
  371. {
  372.     OSErr    anErr;
  373.  
  374.     AEDescList    paramList;
  375.     anErr = AECreateList( nil, 0, false, ¶mList );
  376.     if ( anErr == noErr )
  377.     {
  378.         anErr = AEPutPtr( ¶mList, 0, typeInteger, ¶m1, sizeof( param1 ) );
  379.         if ( anErr == noErr )
  380.         {
  381.             anErr = AEPutPtr( ¶mList, 0, typeInteger, ¶m2, sizeof( param2 ) );
  382.             if ( anErr == noErr )
  383.             {
  384.                 anErr = AEPutParamDesc( theEvent, keyDirectObject, ¶mList );
  385.             }
  386.         }
  387.     }
  388.     
  389.     AEDisposeDesc( ¶mList );
  390.     return ( anErr );
  391. }// end AddTwoIntListToEvent
  392.  
  393. */
  394.